<template>

  <div class="app-container">

    <h2 style="text-align: center;">发布新文章</h2>

    <el-steps :active="1" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写文章基本信息"/>
      <el-step title="编写文章内容"/>
      <el-step title="最终发布"/>
    </el-steps>

    <el-form label-width="120px">

        <el-form-item label="文章标题">
            <el-input v-model="blogInfo.title" placeholder=" 示例：机器学习：从基础到搭建项目"/>
        </el-form-item>

        <!-- 所属分类 TODO -->
        <el-form-item label="文章分类">
            <el-select
                v-model="blogInfo.subjectParentId"
                placeholder="一级分类" @change="subjectLevelOneChanged">

                <el-option
                    v-for="subject in subjectOneList"
                    :key="subject.id"
                    :label="subject.title"
                    :value="subject.id"
                    />

            </el-select>

            <!-- 二级分类 -->
             <el-select v-model="blogInfo.subjectId" placeholder="二级分类" >
                <el-option
                    v-for="subject2 in subjectTwoList"
                    :key="subject2.id"
                    :label="subject2.title"
                    :value="subject2.id"/>
            </el-select>
        </el-form-item> 



        <el-form-item label="文章作者">
        <el-select
            v-model="blogInfo.author"
            placeholder="请选择">

            <el-option
                v-for="author in authorList"
                :key="author.id"
                :label="author.nickname"
                :value="author.id"/>

        </el-select>
        </el-form-item> 
        



        <!-- 文章封面-->
        <el-form-item label="文章封面">

            <el-upload
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                :action="BASE_API+'/useross/fileoss/cover'"
                class="avatar-uploader">
                <img :src="blogInfo.cover">
            </el-upload>

        </el-form-item>

        <el-form-item label="文章评论状态">
         <el-select v-model="blogInfo.commentStatus" clearable placeholder="请选择">
          <el-option :value=0 label="可评论"/>
          <el-option :value=1 label="不可评论"/>
        </el-select>
        </el-form-item>

        <el-form-item>
            <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存并下一步</el-button>
        </el-form-item>
        </el-form>
  </div>
</template>


<script>
import blog from '@/api/tingting/blog'
import subject from '@/api/tingting/subject'
import Tinymce from '@/components/Tinymce'
export default {
    //声明组件

    data(){
        return{

            saveBtnDisabled:false,
            blogInfo:{
                subjectId:'',
                cover:'/static/01.png'
            },
            blogId:"0",
            authorList:[],//封装所有作者
            subjectOneList:[],
            subjectTwoList:[],
            BASE_API:process.env.BASE_API
        }
    },
    created(){
            //获取路由Id值
        if(this.$route.params && this.$route.params.id) {
            this.blogId = this.$route.params.id
            this.getBlog()
          
          
        }else{
            this.getListAuthor()
            this.getOneSubject()    
        }
            
         
           
    },
    methods:{
        //根据id查询文章
        getBlog(){
            blog.getBlogById(this.blogId)
            .then(response => {
                this.blogInfo = response.data.blog
                subject.getSubjectList()
                .then(response => {
                    this.subjectOneList = response.data.list
                    for(var i=0;i<this.subjectOneList.length;i++) {
                                //获取每个一级分类
                                var oneSubject = this.subjectOneList[i]
                                //比较当前courseInfo里面一级分类id和所有的一级分类id
                                if(this.blogInfo.subjectParentId == oneSubject.id) {
                                    //获取一级分类所有的二级分类
                                    this.subjectTwoList = oneSubject.children
                                }
                            }
                })
                this.getListAuthor()
              
            })
        },
        //上传成功：
        handleAvatarSuccess(res,file){
            this.blogInfo.cover = res.data.url
        },
        //上传之前
        beforeAvatarUpload(file){
            // const isJPG = file.type === 'image/jpeg'
            // const isLt10M = file.size / 1024 / 1024 < 10

            // if (!isJPG) {
            //     this.$message.error('上传头像图片只能是 JPG 格式!')
            // }
            // if (!isLt2M) {
            //     this.$message.error('上传头像图片大小不能超过 10MB!')
            // }
            // return isJPG && isLt10M
        },
            //查询所有的二级分类
        subjectLevelOneChanged(value){
            //value是框架封装好的，是一级分类的id值
           
            for(var i = 0;i<this.subjectOneList.length;i++){
                var oneSubject =this.subjectOneList[i]
                if(value === oneSubject.id){
                    this.subjectTwoList = oneSubject.children
                    this.blogInfo.subjectId=''
                }
            }
           
        },
        //查询所有的一级分类
        getOneSubject(){
            subject.getSubjectList()
                .then(response => {
                    this.subjectOneList = response.data.list
                })
        },

    

        //查询所有的作者
        getListAuthor(){
            blog.getListAuthor()
            .then(response => {
                this.authorList=response.data.items
            })
        },
        //添加文章基本信息
        addBlog(){
            blog.addBlogInfo(this.blogInfo)
            .then(response => {
                this.$message({
                    type:'success',
                    message:'添加课程信息成功！'
                });
                this.$router.push({path:'/blog/chapter/'+response.data.blogId})
            })
        },
        //修改文章基本信息
        updateBlog(){
            blog.updateBlog(this.blogInfo)
             .then(response => {
                this.$message({
                    type:'success',
                    message:'修改课程信息成功！'
                });
                this.$router.push({path:'/blog/chapter/'+this.blogId})
            })
        },
        saveOrUpdate(){
            if(!this.blogInfo.id){
                this.addBlog()
            }else{
                this.updateBlog()
            }
         
        }
    }
}
</script>
<style scoped>
.tinymce-container{
    line-height: 29px;
}
</style>